/**
 * Thing对象
 * @author keluo <pycorvn@yeah.net>
 * @since 2012-12-14 10:38:27
 */
var SingleThing = {
		
	init : function(thing_id) {
		var _this = this;
		_this.attach(thing_id);
		var $thing = this.getThingObj(thing_id);
		var thing_type = this.getThingType(thing_id);
		switch(thing_type) {
			case 'video':
				$thing.find('.video .play').click();
			break;
			case 'music':
				$thing.find('.music .play').click();
			break;
		}
	},
	
	// 为指定thing附加事件
	attach : function(thing_id) {
		var $thing = this.getThingObj(thing_id);
		var thing_type = this.getThingType(thing_id);
		this.attachCommon(thing_id);
		switch(thing_type) {
			case 'video':
				this.attachVideo(thing_id);
			break;
			case 'music':
				this.attachMusic(thing_id);
			break;
			case 'photo':
				this.attachPhoto(thing_id);
			break;
		}
	},
	
	// 注册公共事件
	attachCommon : function(thing_id) {
		var $thing = this.getThingObj(thing_id);
		$thing.hover(function() {
			$thing.find('.timeline').css('visibility', 'visible');
			$thing.find('.opt .edit, .opt .delete').show();
		}, function(){
			$thing.find('.timeline').css('visibility', 'hidden');
			$thing.find('.opt .edit, .opt .delete').hide();
		});
	},
	
	attachVideo : function(thing_id) {
		var $thing = this.getThingObj(thing_id);
		
		var recreate = function() {
			$player = $thing.find('.play');
			return '\
			<div>\
				<embed src="'+$player.attr('data-video-url')+'"\
					type="application/x-shockwave-flash" width="'+$player.attr('data-video-width')+'"\
					height="'+$player.attr('data-video-height')+'" wmode="transparent" play="true"\
					loop="false" menu="false" allowscriptaccess="never">\
				</embed>\
			</div>';
		};
		
		$thing.find('.video .play').click(function() {
			$thing.find('.video img').click();
		});
		
		$thing.find('.video img').hover(function() {
			$(this).stop().animate({'opacity': 0.7});
		}, function() {
			$(this).stop().animate({'opacity': 1});
		}).click(function() {
			var $player = $thing.find('.player');
			if($player.is(':hidden')) {
				$(this).parent().hide();
				var html = recreate();
				html += '<div><a href="javascript:;" class="turnup"></a><a href="javascript:;" class="reload"></a></div>';
				$player.hide().html(html).fadeIn();
			} else {
				var _this = $(this);
				$player.animate({height:0}, 'normal', 'swing', function() {
					$(this).css({'height':''}).empty().hide();
					_this.parent().slideDown();
				});
			}
			
			$player.find('.turnup').die().live('click', function() {
				$thing.find('.video img').click();
			});
			
			$player.find('.reload').die().live('click', function() {
				$player.children().eq(0).fadeOut(function() {
					$(this).remove();
					$player.prepend(recreate());
				})
			});
		});
	},
	
	attachMusic : function(thing_id) {
		var $thing = this.getThingObj(thing_id);
		
		// Common(thing_id);
		
		var recreate = function() {
			$player = $thing.find('.play');
			return '\
			<div>\
				<embed src="'+$player.attr('data-music-url')+'"\
					type="application/x-shockwave-flash" width="'+$player.attr('data-music-width')+'"\
					height="'+$player.attr('data-music-height')+'" wmode="transparent" play="true"\
					loop="false" menu="false" allowscriptaccess="never">\
				</embed>\
			</div>';
		};
		
		$thing.find('.music .play').click(function() {
			$thing.find('.music img').click();
		});
		
		$thing.find('.music img').hover(
			function() {
				$(this).stop().animate({'opacity': 0.7});
			}, function() {
				$(this).stop().animate({'opacity': 1});
			}	
		).click(function() {
			var $player = $thing.find('.player');
			if($player.is(':hidden')) {
				var html = recreate();
				html += '<div><a href="javascript:;" class="turnup" title="收起"></a><a href="javascript:;" class="reload" title="重载"></a></div>';
				$player.hide().html(html).fadeIn();
			} else {
				var _this = $(this);
				$player.animate({height:0, width:0}, 'normal', 'swing', function() {
					$(this).css({'height':'', 'width':''}).empty().hide();
					_this.parent().slideDown();
				});
			}
			
			$player.find('.turnup').die().live('click', function() {
				$thing.find('.music img').click();
			});
			
			$player.find('.reload').die().live('click', function() {
				$player.children().eq(0).fadeOut(function() {
					$(this).remove();
					$player.prepend(recreate());
				})
			});
		});
	},
	
	attachPhoto : function(thing_id) {
		var $thing = this.getThingObj(thing_id);
		$thing.find('.medium img').hover(function() {
			$(this).stop().animate({'opacity': 0.7});
		}, function() {
			$(this).stop().animate({'opacity': 1});
		});
		$thing.find('.fancybox[rel="group'+thing_id+'"]').fancybox({
			openEffect	: 'none',
			closeEffect	: 'none'
		});
		$thing.find('.medium img').click(function() {
			$thing.find('.fancybox').eq(0).click();
		});
	},
	
	// 获取指定thing节点对象
	getThingObj : function(thing_id) {
		return $('.thing[data-thing-id='+thing_id+']');
	},
	
	getThingType : function(thing_id) {
		return this.getThingObj(thing_id).attr('data-thing-type');
	}
};

//文本框高度自适应
$.fn.autosize = function(options) {
    var defaults={
        maxHeight:null,
        minHeight:$(this).height()
    };
    var opts = $.extend({}, defaults, options);
    return $(this).each(function() {
        $(this).bind('paste cut keydown keyup focus blur',function(){
            var height,style = this.style;
            this.style.height = opts.minHeight + 'px';
            if (this.scrollHeight > opts.minHeight) {
                if (opts.maxHeight && this.scrollHeight > opts.maxHeight) {
                    height = opts.maxHeight;
                    style.overflowY = 'scroll';
                } else {
                    height = this.scrollHeight;
                    style.overflowY = 'hidden';
                }
                
                height = $.browser.msie ? (height-11) : height;
                style.height = height + 'px';
            }
        });
    });
};

/**
 * Thing评论对象
 */
ThingComment = {
	thing_id : null,
	page_id : null,
	num_per_page : null,
	
	init : function(option) {
		this.thing_id = option.thing_id;
		this.page_id = option.page_id;
		this.num_per_page = option.num_per_page;
		this.initPage();
	},
	initPage : function() {
		this.attachPage();
	},
	attachPage : function() {
		var _this = this;
		$('#'+this.page_id).find('span a').die().live('click', function() {
			var href = $(this).attr('href');
			var page = href.substr(href.lastIndexOf('/')+1);
			var url = SCABISH.host + '/comment/getComments/thing_id/'+_this.thing_id;
			url += '/page/'+parseInt(page)+'/npp/'+_this.num_per_page;
			$.post(url, null, function(response) {
				if(SCABISH.isOk(response)) {
					var data = SCABISH.obtain(response);
					var comments = data.comments;
					$('.comment').empty();
					var template = '';
					for(var i = 0; i < comments.length; i++) {
						template += _this.getTemplate(comments[i]);
					}
					
					$(template).hide().appendTo($('.comment')).fadeIn();
					$('#'+_this.page_id).html(data.paging);
				}	
			});
			
			return false;
		});
	},
	// 获取指定ID评论信息
	fetchSingle : function(comment_id) {
		var _this = this;
		$.get(SCABISH.host + '/comment/getComment/comment_id/'+comment_id, function(response) {
			if(SCABISH.isOk(response)) {
				var comment = SCABISH.obtain(response);
				$(_this.getTemplate(comment)).hide().prependTo($('.comment')).slideDown();
			}
		});
	},
	// 获取分页评论列表
	fetchPage : function() {
		
	},
	// 获取评论模板
	getTemplate : function(comment) {
		return '\
		<li data-comment-id="'+comment.comment_id+'">\
			<a href="/u/'+comment.user_id+'">\
				<img style="float:left;width:30px;" class="avatar" alt="" src="'+comment.avatar+'">\
			</a>\
			<div style="float:left;margin-left:10px;">\
				<span class="remove-comment"></span>\
				<div class="content">\
					<a class="bbl-name-card" href="/u/'+comment.user_id+'" data-uid="'+comment.user_id+'">'+comment.nickname+'</a>\
					'+comment.text+'\
				</div>\
				<div class="footer">\
					<span class="timestamp" timestamp="'+comment.post_date+'" style="color:#ddd;text-shadow:none;font-size:12px;">'+comment.post_date+'</span>\
					<a class="reply-echo" href="javascript:;" style="display:none;">回复</a>\
				</div>\
			</div>\
			<div style="clear:both;"></div>\
		</li>';
	}
};

/**
 * 评论表单对象
 */
var ReplyForm = {
    thing_id : null,
	form_id : null,
	processing : false, // 是否正在处理
	
	init : function(option) {
		this.form_id = option.form_id;
		this.thing_id = option.thing_id;
		var _this = this;
		$('#'+this.form_id).find('textarea').autosize();
		$('#'+this.form_id).find('input[type="button"]').click(function() {
			_this.post();
		});		
	},
	beforePost : function() {
		if(this.processing) return false;
		var $text = $('#'+this.form_id).find('textarea');
		if($.trim($text.val()) == '') {
			$text.val('').focus(); return false;
    	}
		this.processing = true;
		$('#'+this.form_id).find('input[type="button"]').addClass('disabled');
		return true;
	},
	afterPost : function() {
		this.processing = false;
		$('#'+this.form_id).find('input[type="button"]').removeClass('disabled');
	},
	post : function() {
		if(!this.beforePost()) return;
		var _this = this;
    	var url = SCABISH.host + '/comment/post/thing_id/'+this.thing_id;
    	$.post(url, $('#'+this.form_id).serialize(), function(response){
    		if(SCABISH.isOk(response)) {
    			 $('#'+_this.form_id).find('textarea').val('');
				ThingComment.fetchSingle(SCABISH.obtain(response));
			} else { // 返回失败代码
				var code = SCABISH.obtain(response);
				if(code == 'user_not_login') { // 未登录
					LOGIN.show(function() {
						if(LOGIN.success) { // 登录成功
							_this.post();
						} else { // 被取消登录
							
						}
					});
				} else { // 其他未知问题
					//SCABISH.error(code)
				}
			}
    		_this.afterPost();
        });
	}
};

// 设置背景图
$.get(SCABISH.host+'/api/background', function(response) {
	//document.body.style.background = '#eee url('+response+') no-repeat fixed top';
});